
/***
 * Copyright (c) 2012 Centrodata Sistemas e Automação - www.centrodata.com.br
 * All rights reserved.
 */

package br.com.centrodata.controller;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.caelum.vraptor.validator.Validations;
import br.com.centrodata.dao.UserDao;
import br.com.centrodata.interceptor.Public;
import br.com.centrodata.interceptor.SessionInfo;
import br.com.centrodata.model.User;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

/**
 *
 * @author João Fernandes Lima Neto
 */
@Resource
public class HomeController {

    private final Result      result;
    private final Validator   validator;
    private final SessionInfo sessionInfo;
    private final UserDao     dao;

    public HomeController(UserDao dao, SessionInfo sessionInfo, Result result, Validator validator) {
        this.dao         = dao;
        this.result      = result;
        this.validator   = validator;
        this.sessionInfo = sessionInfo;
    }

    @Post
    @Public
    public void login(String login, String senha) {
        final User currentUser = dao.login(login, senha);

        validator.checking(new Validations() {
            {
                that(currentUser, is(notNullValue()), "login", "invalid_login_or_password");
            }
        });
        validator.onErrorUsePageOf(this).login();

        sessionInfo.login(currentUser);

        /*
         * O Controle do menu será feito pelo JSP
         */
        result.redirectTo(MenuController.class).menu();
    }

    public void logout() {
        sessionInfo.logout();
        result.redirectTo(this).login();
    }

   
    @Public
    @Get
    public void login() {
    }
}